﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LearnDrawing.Controls
{
    /// <summary>
    /// WithDrawingGroup.xaml 的交互逻辑
    /// </summary>
    public partial class WithDrawingGroup : UserControl
    {
        public WithDrawingGroup()
        {
            InitializeComponent();
        }

        public string DrawingTime
        {
            get { return (string)GetValue(DrawingTimeProperty); }
            set { SetValue(DrawingTimeProperty, value); }
        }

        public static readonly DependencyProperty DrawingTimeProperty =
            DependencyProperty.Register("DrawingTime", typeof(string), typeof(WithDrawingGroup), new UIPropertyMetadata("", null));



        public WaveData WaveData
        {
            get { return (WaveData)GetValue(WaveDataDataProperty); }
            set { SetValue(WaveDataDataProperty, value); }
        }

        public static readonly DependencyProperty WaveDataDataProperty =
            DependencyProperty.Register("WaveData", typeof(WaveData), typeof(WithDrawingGroup), new UIPropertyMetadata(null, WaveDataPropertyChanged));

        private static void WaveDataPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs arg)
        {
            WithDrawingGroup? control = obj as WithDrawingGroup;
            WaveData Data = (WaveData)arg.NewValue;

            if (Data != null)
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                control?.DrawWaveImage(Data);
                stopwatch.Stop();
                control.DrawingTime = $"Using: {stopwatch.ElapsedMilliseconds}ms";
            }
        }

        private void DrawWaveImage(WaveData Data)
        {
            DrawingGroup group = new DrawingGroup();
            using (DrawingContext ctx = group.Open())
            {
                DrawWaveByContext(ctx, Data);
            }
            group.Freeze();
            DrawingImage drawimage = new DrawingImage(group);
            this.imageWave.Source = drawimage;
        }

        private void DrawWaveByContext(DrawingContext ctx, WaveData Data)
        {
            int PicWidth = 1000;
            int PicHeight = 400;

            //绘制外框
            ctx.DrawRectangle(Brushes.Black, new Pen(Brushes.White, 1), new Rect(0, 0, PicWidth, PicHeight));

            //判定数据有效性
            if (Data == null)
            {
                return;
            }

            Pen penRect = new Pen(Brushes.LightGreen, 1);

            int x2 = 0, y2 = 0;
            for (int i = 0; i < 1900; i++)
            {
                int x1 = i;
                int y1 = Data.Datas[i];

                ctx.DrawEllipse(null, penRect, new Point(x1 / 2 + 15, y1 + 15), 30, 30);
            }           
        }
    }
}
